using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace flickr.api
{
	public class Utilities
	{
        private Utilities(){}
        private static readonly DateTime unixStartDate = new DateTime(1970, 1, 1, 0, 0, 0);

        public static string Md5Hash(string unhashed)
        {
            MD5CryptoServiceProvider csp = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(unhashed);
            byte[] hashedBytes = csp.ComputeHash(bytes, 0, bytes.Length);
            return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
        }

        public static long DateToUnixTimestamp(DateTime theDate)
        {
            TimeSpan ts = theDate - unixStartDate;
            return (int)ts.TotalSeconds;
        }

        public static DateTime UnixTimestampToDate(string theTimestamp)
        {
            return UnixTimestampToDate(long.Parse(theTimestamp));
        }

        public static DateTime UnixTimestampToDate(long theTimestamp)
        {
            return unixStartDate.AddSeconds(theTimestamp);
        }

        public static string UrlEncode(string oldString)
        {
            string a = HttpUtility.UrlEncode(oldString);
            a = a.Replace("&", "%26");
            a = a.Replace("=", "%3D");
            a = a.Replace(" ", "%20");
            return a;
        }
	}

}
